package cn.icanci.ladybird.service.user.impl;

import cn.icanci.ladybird.pojo.dto.UserProduceDTO;
import cn.icanci.ladybird.pojo.entity.*;
import cn.icanci.ladybird.service.*;
import cn.icanci.ladybird.service.user.UserProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: zxp
 * @date: Created in 2020/10/2 0002 下午 20:37
 * @classAction:
 */

@SuppressWarnings("all")
@Service
public class UserProduceServiceImpl implements UserProduceService {
    @Autowired
    private TicketDetailsService ticketDetailsService;
    @Autowired
    private TicketAddressService ticketAddressService;
    @Autowired
    private TicketDetailsTicketAddressMiddleService ticketDetailsTicketAddressMiddleService;
    @Autowired
    private TicketTimeService ticketTimeService;
    @Autowired
    private TicketDetailsTicketTimeMiddleService ticketDetailsTicketTimeMiddleService;
    @Autowired
    private TicketPriceService ticketPriceService;
    @Autowired
    private TicketDetailsTicketPriceMiddleService ticketDetailsTicketPriceMiddleService;

    @Override
    public UserProduceDTO queryUserProduceDTO(Long ticketId) {

        //根据id查询
        TicketDetails details = ticketDetailsService.queryById(ticketId);
        //创建DTO对象
        UserProduceDTO userProduceDto = new UserProduceDTO();
        //拿到票的id
        Long ticketDetailsId = details.getTicketDetailsId();
        //设置DTO数据对象的id
        userProduceDto.setTicketDetailsId(ticketDetailsId);
        //商品名
        userProduceDto.setTicketDetailsInfo(details.getTicketDetailsInfo());
        //商品图片地址
        userProduceDto.setTicketDetailsTicketImgUrl(details.getTicketDetailsTicketImgUrl());
        //商品介绍图片地址
        userProduceDto.setTicketDetailsTicketImgInfoUrl(details.getTicketDetailsTicketImgInfoUrl());
        //场馆
        userProduceDto.setTicketAddressVenue(details.getTicketAddressVenue());
        //城市
        userProduceDto.setTicketAddressCity(details.getTicketAddressCity());
        // 是否秒杀
        userProduceDto.setTicketDetailsIsKill(details.getTicketDetailsIsKill());
        // 商品状态
        userProduceDto.setTicketDetailsStatus(details.getTicketDetailsStatus());
        // 设置文件地址
        userProduceDto.setTicketDetailsExcelAddress(details.getTicketDetailsExcelAddress());
        // 设置权重值
        userProduceDto.setTicketDetailsWeight(details.getTicketDetailsWeight());
        // 查询时间
        List<TicketDetailsTicketTimeMiddle> timeMiddles = ticketDetailsTicketTimeMiddleService.queryByTicketDetailsId(ticketDetailsId);
        // 遍历集合
        ArrayList<Date> dates = new ArrayList<>();
        for (TicketDetailsTicketTimeMiddle middle : timeMiddles) {
            Long timeMiddleId = middle.getTicketTimeMiddleId();
            TicketTime ticketTime = ticketTimeService.queryById(timeMiddleId);
            dates.add(ticketTime.getTicketTime());

        }

        dates.sort((d1, d2) -> {
            return d1.compareTo(d2);
        });
        userProduceDto.setTicketTime(dates);
        userProduceDto.setTicketStartTime(dates.get(0));
        userProduceDto.setTicketEndTime(dates.get(dates.size() - 1));

        //商品详情和价格中间表
        List<TicketDetailsTicketPriceMiddle> priceMiddles = ticketDetailsTicketPriceMiddleService.queryByTicketDetailsId(ticketDetailsId);
        // 遍历集合
        ArrayList<Object> prices = new ArrayList<>();
        for (TicketDetailsTicketPriceMiddle middle : priceMiddles) {
            Long ticketPriceMiddleId = middle.getTicketPriceMiddleId();
            TicketPrice ticketPrice = ticketPriceService.queryById(ticketPriceMiddleId);
            prices.add(Double.valueOf((Double) ticketPrice.getTicketPricePrice()));
        }

        userProduceDto.setTicketPricePrice(prices);

        return userProduceDto;
    }
}
